<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: NewUser.php 394 2010-12-25 13:23:21Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalUserRecord');
Prado::using('Application.Common.Data.PortalToDoRecord');
Prado::using('Application.Common.XMimeMail');

class NewUser extends PortalSystemPage
{
	public function onInit($param)
	{
		if(!TPropertyValue::ensureBoolean($this->Application->Parameters['MultipleUser']))
		{
			throw new PortalException(500,'newuser_registration_disallowed');
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Title = $this->Application->Parameters['SiteTitle'] . " - " . Prado::localize("Register as user");
		if(!$this->IsPostBack)
		{
			$this->SiteNews->Checked = true;
			$this->dlLanglist->PromptText	= Prado::localize("Select");
			$this->dlLanglist->PromptValue	= 0;
			$this->dlLanglist->DataSource	= $this->DataAccess->createCommand("select culture,name from tbllanguages where active=1")->query();
			$this->dlLanglist->dataBind();
		}
	}

	public function checkUsername($sender,$param)
	{
		$username	= $this->Username->Text;
		$id			= $this->DataAccess->createCommand("select * from tblusers where name='$username'")->queryRow();

		if($id)
		{
			$param->IsValid = false;
		}
		else
		{
			$param->IsValid = true;
		}
	}

	public function checkEmail($sender,$param)
	{
		$email = strtolower($this->Email->Text);
		$id = $this->DataAccess->createCommand("select * from tblusers where email='$email'")->queryRow();

		if($id)
		{
			$param->IsValid = false;
		}
		else
		{
			$param->IsValid = true;
		}
	}

	public function createUser($sender,$param)
	{
		if($this->IsValid)
		{
			$nu = new PortalUserRecord();
			$nu->name		= $this->Username->Text;
			$nu->full_name	= $this->FullName->Text;
			$nu->role		= $this->Application->Parameters['NewUserRole'];
			$nu->password	= hash("sha256",$this->Password->Text);
			$nu->vcode		= PortalUtil::getGUID();
			$nu->email		= $this->Email->Text;
			$nu->reg_time	= time();
			$nu->status		= TPropertyValue::ensureBoolean($this->Application->Parameters['AccountApproval']) ? 2 : 0;
			$nu->sitenews	= TPropertyValue::ensureInteger($this->SiteNews->Checked);
			$nu->culture	= $this->dlLanglist->SelectedValue;
			$nu->save();

			// Create an todo or send email ?
			$nuaction = TPropertyValue::ensureBoolean($this->Application->Parameters['NewAccountAction']);
			switch($nuaction)
			{
				case 0: //nop
					break;

				case 1: //Create todo
					$this->CreateToDo($this->Username->Text);
					break;

				case 2: // Send admin email
					$this->SendAdminMail($this->Username->Text);
					break;

				case 3: // Send admin email and create todo
					$this->CreateToDo($this->Username->Text);
					$this->SendAdminMail($this->Username->Text);
					break;
			}

			if(TPropertyValue::ensureBoolean($this->Application->Parameters['AccountApproval']))
			{
				$this->gotoPage('System.NewUserCreated');
			}
			else
			{
				//$this->gotoPage('System.Login');
				$authManager=$this->Application->getModule('auth');
				if(!$authManager->login($this->Username->Text,$this->Password->Text))
				{
					$this->reportError(0,"Faild to log you in");
				}
				$this->gotoDefaultPage();
			}
		}
	}

    public function regenerateToken($sender,$param)
    {
        $this->Captcha->regenerateToken();
    }

	private function CreateToDo($username)
	{
		$todo = new PortalToDoRecord();
		$todo->title		= Prado::localize('New user created');
		$todo->description	= Prado::localize('New user created with name {0}',array($username));
		$todo->created		= time();
		$todo->deadline		= time() + (60*60*60*24);
		$todo->priority		= 1;
		$todo->assignedto	= 1;
		$todo->assignedby	= 1;
		$todo->createdby	= 1;
		$todo->save();
	}

	private function SendAdminMail($username)
	{
		$to_email	= $this->Application->Parameters['AdminEmail'];
		$to_name	= $this->Application->Parameters['SiteOwner'];
		$xMail		= new XMimeMail();
		$xMail->setCharset('iso-8859-1');
		$xMail->setHtmlBody(Prado::localize('New user created with name {0}',array($username)),'');
		$params['html_encoding'] = 'quoted-printable';
		$params['text_encoding'] = 'quoted-printable';
		$params['text_wrap'] = 998;
		$xMail->prepare($params);
		try
		{
			$xMail->send($to_name, $to_email, "Prado portal", $this->Application->Parameters['AdminEmail'], Prado::localize('New user created'));
		}
		catch(Exception $e)
		{
			$this->reportError(0, $e->getMessage());
		}
	}
}
?>